<?php

namespace app\model\user;

use app\base\BaseModel;

/**
 * 模型-UserBeanPredict
 * Class UserBeanPredictModel
 * @package app\model
 */
class UserEarningsPredictModel extends BaseModel
{

    protected $name = 'user_earnings_predict';

    /**
     * 范围查询-status
     *
     * @param [type] $query
     * @param [type] $status
     * @return void
     */
    public function scopeUserId($query, $user_id)
    {
        if (!empty($user_id)) {
            $query->where('user_id', $user_id);
        }
    }

    /**
     * 分页查询
     * @param string $user_id
     * @param $current_page
     * @param $list_rows
     * @return array
     */
    public function getPageData($user_id = '', $current_page, $list_rows)
    {

        $dataList = $this
            ->userId($user_id)
            ->order('add_time DESC')
            ->paginate(['page' => $current_page, 'list_rows' => $list_rows], false);
        $current = $dataList->currentPage();
        $total = $dataList->total();
        $limit = $dataList->listRows();
        $items = $dataList->items();
        return ['total' => $total, 'current' => $current, 'limit' => $limit, 'dataList' => $items];
    }

    /**
     * 添加预计收益记录
     * @param $user_id
     * @param $price
     * @param string $day
     * @param string $detail
     * @param int $item_id
     * @return array
     */
    public static function addEarnings($user_id, $price, $day = '', $detail = "", $item_id = 0)
    {
        if ($price <= 0) {
            return [
                'code' => 1,
                'msg' => '操作成功'
            ];
        }
        $in_data = [
            'add_time' => time(),
            'day' => $day,
            'user_id' => $user_id,
            'price' => $price,
            'detail' => $detail,
            'order_item_id' => $item_id
        ];
        $ret = self::insert($in_data);
        if ($ret === false) {
            return [
                'code' => 0,
                'msg' => '操作失败'
            ];
        }
        return [
            'code' => 1,
            'msg' => '操作成功'
        ];
    }

    /**
     * 获取预收益的累计
     * @param string $user_id
     * @param string $day
     * @return float|int
     */
    public static function getEarningsSum($user_id = '', $day = '')
    {
        $map = [
            'user_id' => $user_id,
            'day' => $day
        ];
        $price = self::where($map)->sum('price');
        if ($price > 0) {
            return round($price, 2);
        }
        return 0;
    }

    /**
     * 获取预收益的累计(总的)
     * @param string $user_id
     * @return float|int
     */
    public static function getEarningsAllSum($user_id = '')
    {
        $map = [
            'user_id' => $user_id
        ];
        $price = self::where($map)->sum('price');
        if ($price > 0) {
            return round($price, 2);
        }
        return 0;
    }


    /**
     * 清除已经结算的数据
     * @param $item_id
     * @return bool
     */
    public static function clearEarnings($item_id = 0)
    {
        $ret = self::where('order_item_id', $item_id)->delete();
        if ($ret === false) {
            return false;
        }
        return true;
    }


}
